﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Drawing.Drawing2D;
using BusinessLogic;
using BusinessObject;
using Utility;
using System.Web.Security;
using System.Resources;
namespace WebTeamplate
{
    public partial class Admin_PopupPrintPhieuNhapHang : BasePage
    {
        public string nguoiNhap = string.Empty;
        public string kho = string.Empty;
        public string curencyName = string.Empty;
        public PhieuNhapHang objPhieuNhapHang = new PhieuNhapHang();
        public double total = 0;
        public Int64 MaPhieuPhap
        {

            get
            {
                object o = ViewState["MaPhieuPhap"];
                if (o != null) return (Int64)o;
                return Int64.MinValue;
            }

            set { ViewState["MaPhieuPhap"] = value; }

        }
        #region Init,Load
        protected void Page_Load(object sender, EventArgs e)
        {

            //  ClientDefaultButton.SetPageDefaultButton(this.Page, btnSearch.ClientID);
            if (!Page.IsPostBack)
            {

                InitParamater();

            }
            LoadData();
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
        private void InitParamater()
        {
            MaPhieuPhap = ConvertValueObject.ParseInt(CommonFunction.GetValueFromQueryString(this.Page, "id"));
        }
        #endregion

        #region Methods
        public string GetXe(string maxe)
        {
            DMXe objXe = DMXeUtil.Get(ConvertValueObject.ParseInt(maxe));
            if (objXe != null)
                return objXe.TenXe;
            else return string.Empty;
        }
        public void LoadData()
        {

            objPhieuNhapHang = PhieuNhapHangUtil.Get(this.MaPhieuPhap);
            if (objPhieuNhapHang != null)
            {
                Curency objCurency = PhieuNhapHangUtil.GetCurencyForPhieuNhapHang(this.MaPhieuPhap);
                if (objCurency != null)
                    curencyName = objCurency.Name;
                MembershipUser member = Membership.GetUser(objPhieuNhapHang.NguoiNhap);
                if (member != null)
                {
                    UserProfile objProfile = UserProfileUtil.Get(this.applicationName, new Guid(member.ProviderUserKey.ToString()));
                    if (objProfile != null)
                        nguoiNhap = objProfile.Name;
                    else nguoiNhap = objPhieuNhapHang.NguoiNhap;
                }
                else nguoiNhap = objPhieuNhapHang.NguoiNhap;
                //get Kho
                SubCategory objKho = SubCategoryUtil.Get(objPhieuNhapHang.MaKho, CommonFunction.DEFAULT_WAREHOUSE_CODE);
                kho = objKho.SubCategoryName;
                //get List Data
                List<ChiTietPhieuNhapHangDetail> lstChiTiet = ChiTietPhieuNhapHangUtil.GetChiTietPhieuNhapHangDetailByMaPhieuNhapHang(this.MaPhieuPhap);
                //generate form here
                rptData.DataSource = lstChiTiet;
                rptData.DataBind();
                ltrTotalString.Text = CommonFunction.ReadNumber(ConvertValueObject.ParseString(total));
            }
        }

        #endregion

        #region Events,Handler

        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ChiTietPhieuNhapHangDetail cate = (ChiTietPhieuNhapHangDetail)e.Item.DataItem;
                if (cate == null)
                    return;
               // Curency objCurency = CurencyUtil.Get(cate.CurencyCode);
                Literal ltr = (Literal)e.Item.FindControl("ltrGia");
                ltr.Text = ConvertValueObject.ParseString(cate.GiaNhap) + " " + curencyName;

                ltr = (Literal)e.Item.FindControl("ltrSTT");
                ltr.Text = (e.Item.ItemIndex + 1).ToString();
                ltr = (Literal)e.Item.FindControl("ltrThanhTien");
                double thanhtien = cate.SoLuongNhap * cate.GiaNhap;
                ltr.Text = ConvertValueObject.ParseString(thanhtien) + " " + curencyName;
                total += thanhtien;
            }
            else if (e.Item.ItemType == ListItemType.Footer)
            {
                Literal ltr = (Literal)e.Item.FindControl("ltrTotal");
                ltr.Text = ConvertValueObject.ParseString(total);
            }
        }

        #endregion

             

    }
}
